package com.fire.transaction.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fire.common.domain.BusinessException;
import com.fire.common.domain.CommonErrorCode;
import com.fire.transaction.api.dto.PlatformChannelDTO;
import com.fire.transaction.api.dto.com.fire.transaction.api.PayChannelService;
import com.fire.transaction.convert.PlatformChannelConvert;
import com.fire.transaction.entity.AppPlatformChannel;
import com.fire.transaction.entity.PlatformChannel;
import com.fire.transaction.entity.PlatformPayChannel;
import com.fire.transaction.mapper.AppPlatformChannelMapper;
import com.fire.transaction.mapper.PlatformChannelMapper;
import com.fire.transaction.mapper.PlatformPayChannelMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author fire
 * @date 2021年07月21日17:21
 */
@DubboService
public class PayChannelServiceImpl implements PayChannelService {

    @Autowired
    private PlatformChannelMapper platformChannelMapper;

    //应用和服务类型联合表的mapper
    @Autowired
    private AppPlatformChannelMapper appPlatformChannelMapper;

    /**
     * 查询平台的服务类型
     *
     * @return
     * @throws BusinessException
     */
    @Override
    public List<PlatformChannelDTO> queryPlatformChannel() throws BusinessException {
        List<PlatformChannel> platformChannels = platformChannelMapper.selectList(null);
        return PlatformChannelConvert.INSTANCE.listentity2listdto(platformChannels);
    }

    /**
     * 为某个应用绑定平台服务类型
     *
     * @param appId
     * @param platformChannelCode
     * @throws BusinessException
     */
    @Override
    @Transactional
    public void bindPlatformChannelForApp(String appId, String platformChannelCode) throws BusinessException {
//
//        1.现根据应用id和服务类型code判断是否已存在，有则不再插入
        AppPlatformChannel appPlatformChannel = appPlatformChannelMapper.selectOne(new LambdaQueryWrapper<AppPlatformChannel>()
                .eq(AppPlatformChannel::getAppId, appId)
                .eq(AppPlatformChannel::getPlatformChannel, platformChannelCode));
        if (appPlatformChannel!=null){
            throw new BusinessException(CommonErrorCode.E_300008);
        }
//
        AppPlatformChannel entity = new AppPlatformChannel();
        entity.setAppId(appId);//应用ID
        entity.setPlatformChannel(platformChannelCode);//服务类型code
        appPlatformChannelMapper.insert(entity);
    }

    /**
     * 应用绑定服务的状态
     *
     * @param appId
     * @param platformChannelCode
     * @return 为0表示，未绑定；1，表示已绑定
     * @throws BusinessException
     */
    @Override
    public int queryAppBindPlatformChannel(String appId, String platformChannelCode) throws BusinessException {
        AppPlatformChannel appPlatformChannel = appPlatformChannelMapper.selectOne(new LambdaQueryWrapper<AppPlatformChannel>()
                .eq(AppPlatformChannel::getAppId, appId)
                .eq(AppPlatformChannel::getPlatformChannel, platformChannelCode));
        if (appPlatformChannel!=null){
            return 1;
        }
        return 0;
    }

}
